package vns.client.presenter;

import java.util.ArrayList;
import java.util.List;

import vns.client.VNSmartUI;
import vns.client.event.ObjectTypeEditEvent;
import vns.client.helper.RPCCall;
import vns.client.service.ObjectTypeServiceAsync;
import vns.client.view.ObjectTypeUI;
import vns.shared.ObjectTypeDTO;


import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

public class ObjectTypePresenter implements Presenter{

	@Override
	public void go(HasWidgets container) {
		container.clear();
		container.add(display.asWidget());
		doInitTable();
	}

	public interface Display{
		Button getUpdateButton();
		Button getEditButton();
		
		ObjectTypeDTO getCurrentObjectTypeDTO();
		ArrayList<ObjectTypeDTO> getListRow();
		boolean setListRow(List<ObjectTypeDTO> listRow);
		
		Widget asWidget();
		void render();
		
		HTMLPanel getHTMLPanelDetail();
	}

	private final Display display;
	private final SimpleEventBus eventBus;
	private final ObjectTypeServiceAsync rpcService;
	
	/*
	 * Public method
	 */
	
	public ObjectTypePresenter(ObjectTypeUI view, SimpleEventBus eventBus, ObjectTypeServiceAsync model){
		this.display = view;
		this.eventBus = eventBus;
		this.rpcService= model;
		bind();
	}
	
	/*
	 * Private method
	 */
	
	private boolean bind(){
		display.getUpdateButton().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				doUpdate();
			}
		});
		
		display.getEditButton().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				eventBus.fireEvent(new ObjectTypeEditEvent(display.getCurrentObjectTypeDTO()));
			}
		});
		return false;
	}
	
	private void doInitTable() {
//		Window.alert("doInitTable()");
		new RPCCall<ArrayList<ObjectTypeDTO>>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Không load được kiểu danh sách!"
						+ caught.toString());
			}
			
			@Override
			public void onSuccess(ArrayList<ObjectTypeDTO> result) {
//				Window.alert("Tải dữ liệu thành công");
				display.getListRow().clear();
				display.getListRow().addAll(result);
				display.render();
//				Window.alert("NumRow Result = "
//						+ Integer.toString(result.size()));
//				Window.alert("NumRow ListRow = "
//						+ Integer.toString(display.getListRow().size()));
			}
			
			@Override
			protected void callService(
					AsyncCallback<ArrayList<ObjectTypeDTO>> cb) {
//				Window.alert("Loading ..");
				rpcService.Loadall(cb);
//				Window.alert("Loading .. finish");
			}
		}.retry(3);
//		Window.alert("doInitTable() ... Finish");

	}

	private void doUpdate(){
//		Window.alert("Do Update, các phần từ được update là");
//		ArrayList<ObjectTypeDTO> listNew = display.getListRow();
//		for(ObjectTypeDTO o : listNew){
//			Window.alert(o.getName() + " " + Boolean.toString(o.getIsuser()) + " " + o.getDescribe());
//		}
		new RPCCall<Void>() {
			
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Có lỗi khi đang cập nhật! \n " + caught.toString());
			}
			
			@Override
			public void onSuccess(Void result) {
				Window.alert("Cập nhật thành công! \n ");
				VNSmartUI.get().reloadVpanelList();
				doInitTable();
			}
			
			@Override
			protected void callService(AsyncCallback<Void> cb) {
//				Window.alert("RPC Serive Do Update");
				rpcService.Updateall(display.getListRow(), cb);
			}
		}.retry(3);
	}
}
